package com.cwt.admin.index;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckOr;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cwt.admin.common.BaseController;
import com.cwt.common.result.AjaxResult;
import com.cwt.common.utils.SqlUtils;
import com.cwt.common.vo.PageInfo;
import com.cwt.service.domain.ShopCollect;
import com.cwt.service.service.ShopCollectService;
import com.cwt.service.service.TbBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author chenwt
 * @version V1.0
 * @describe 【收藏图书管理】
 * @date 2023年08月09日 10:18:05
 */
@Api(tags = "购物车相关接口", value = "CollectController")
@RequestMapping("/collect")
@RestController
@SaCheckOr(
        login = @SaCheckLogin,
        role = @SaCheckRole("manage"),
        permission = @SaCheckPermission("goods")
)
public class CollectController extends BaseController {

    @Autowired
    private ShopCollectService collectService;

    @Autowired
    private TbBookService bookService;

    @ApiOperation(value = "收藏商品分页", protocols = "http", httpMethod = "POST")
    @RequestMapping(value = "/page", method = {RequestMethod.GET, RequestMethod.POST})
    public @ResponseBody
    AjaxResult shopCarPage(@RequestBody PageInfo info) {
        Page<ShopCollect> page = new Page<>(info.getPageNum(), info.getPageSize());
        QueryWrapper<ShopCollect> queryWrapper = new QueryWrapper<>();
        SqlUtils.wrapperSetLikeCondition(queryWrapper, info.getParams());
        page = collectService.page(page, queryWrapper);
        page.getRecords().forEach(collect -> collect.setBook(bookService.getById(collect.getBookId())));
        return AjaxResult.success(page);
    }

    @ApiOperation(value = "收藏商品添加", protocols = "http", httpMethod = "POST")
    @RequestMapping(value = "/add", method = {RequestMethod.GET, RequestMethod.POST})
    public @ResponseBody AjaxResult addShopCar(@RequestBody ShopCollect collect) {
        return collectService.addCollect(collect);
    }

    @ApiOperation(value = "收藏商品移除", protocols = "http", httpMethod = "POST")
    @RequestMapping(value = "/remove", method = {RequestMethod.GET, RequestMethod.POST})
    public @ResponseBody AjaxResult removeShopCar(@RequestBody String id) {
        return AjaxResult.success(collectService.removeById(id));
    }

}
